package com.example.calladapter;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

/**
 * @author GYL
 * @version 1.0
 * @dateTime 2020/6/11-18:07
 * @description
 */
public class CustomCallAdapterFactory extends CallAdapter.Factory {
  
  public static CustomCallAdapterFactory create() {
    return new CustomCallAdapterFactory();
  }
  
  @Override
  public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    
    //获取原始类型
    Class<?> rawType = getRawType(returnType);
    
    //返回值必须是Custom并且带有泛型（参数类型），根据APIService接口中的方法返回值，确定returnType
    //如 CustomCall<String> getCategories();，那确定returnType就是CustomCall<String>
    if (rawType == CustomCall.class && returnType instanceof ParameterizedType) {
      Type callReturnType = getParameterUpperBound(0, (ParameterizedType) returnType);
      return new CustomCallAdapter(callReturnType);
    }
    return null;
  }
}
